package kr.co.doriclassic.mypuppystory.searchpuppy;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import kr.co.doriclassic.mypuppystory.R;
import kr.co.doriclassic.mypuppystory.alarmpuppy.PreferenceManager;
import kr.co.doriclassic.mypuppystory.searchpuppy.ItemParser.ParsingXML;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

//변경할것
public class SidoParser extends Parser<Sido> {
	ArrayList<Sido> item;
	private XmlPullParser parser;
	private Context context;
	private Spinner list;

	public SidoParser(String url, Context context, Spinner list) {
		this.context = context;
		this.list = list;
		new ParsingXML().execute(url);
	}

	@Override
	ArrayList<Sido> parseItem(String url) {
		parser = connInfo(url);
		item = new ArrayList<Sido>();
		try {
			String tag;
			Sido itemClass = null;
			boolean init_orgCd = false;
			boolean init_orgdownNm = false;
			int parserEvent = parser.getEventType();
			while (parserEvent != XmlPullParser.END_DOCUMENT) {
				switch (parserEvent) {
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					// item 불러내기
					if (parser.getName().equalsIgnoreCase("item")) {
						itemClass = new Sido();
					}
					if (parser.getName().equalsIgnoreCase("orgCd")) {
						init_orgCd = true;
					} else if (parser.getName().equalsIgnoreCase("orgdownNm")) {
						init_orgdownNm = true;
					}
					break;
				case XmlPullParser.TEXT:
					if (init_orgCd) {
						itemClass.setOrgCd(parser.getText());
						init_orgCd = false;
					} else if (init_orgdownNm) {
						itemClass.setOrgdownNm(parser.getText());
						init_orgdownNm = false;
					}
					break;
				case XmlPullParser.END_TAG:
					tag = parser.getName();
					if (tag.equalsIgnoreCase("item")) {
						item.add(itemClass);
					}
					break;
				}
				parserEvent = parser.next();
			}

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return item;
	}

	public class ParsingXML extends AsyncTask<String, String, ArrayList<Sido>> {

		protected void onPreExecute() {
		}

		@Override
		protected void onPostExecute(ArrayList<Sido> result) {
			SidoAdapter adapter = new SidoAdapter(context,
					android.R.layout.simple_spinner_dropdown_item, result);

			list.setAdapter(adapter);
		}

		@Override
		protected ArrayList<Sido> doInBackground(String... params) {
			return parseItem(params[0]);
		}
	}

	// 변경할것
	public class SidoAdapter extends BaseAdapter implements SpinnerAdapter {

		Context context;
		LayoutInflater inflater;
		String url;
		private int spinner;
		ArrayList<Sido> item;

		public SidoAdapter(Context context, int spinner, ArrayList<Sido> item) {
			this.item = item;
			this.spinner = spinner;
			this.context = context;

			Sido k = new Sido();
			k.setOrgdownNm("+지역+");
			k.setOrgCd("");
			;
			item.add(0, k);

			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return item.size();
		}

		@Override
		public Object getItem(int position) {
			return item.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				inflater = (LayoutInflater) context
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(spinner, null);
			}
			TextView tv = (TextView) convertView;
			tv.setText(item.get(position).getOrgdownNm());
			PreferenceManager pf = new PreferenceManager(context);
			pf.savePref("uprCd" + position, item.get(position).getOrgCd());
			return convertView;
		}
	}
}
